﻿/// Write a program that fills and prints a matrix of size (n, n) as shown below: (examples for n = 4)


using System;

class zad1
{
    static void Main()
    {
        int n = 8;
        int[,] matrix = new int[n, n];
        
        // a) by rows
        int k = 1;
        for (int i = 0; i < n; i++)
        {
            EnterCol(n, matrix, ref k, i);
        }

        PrintMarix(n, matrix);

        Console.WriteLine();

        // b) by rows snake
        k = 1;
        for (int i = 0; i < n; i++)
        {
            if (i % 2 == 0)
            {
                EnterCol(n, matrix, ref k, i);
            }
            else
            {
                EnterColReverse(n, matrix, ref k, i);
            }

        }
        PrintMarix(n, matrix);
        Console.WriteLine();

        // c) by diagonals  down-left
        k = 1;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j <= i; j++)
            {
                matrix[n - i + j - 1, j] = k;
                k++;
            }
        }

        for (int i = n-2; i >= 0; i--)
        {
            for (int j = i; j >= 0; j--)
            {
                matrix[i-j, n-j-1] = k;
                k++;
            }
        }

        PrintMarix(n, matrix);
        Console.WriteLine();

        // d) spiral 
        int currentRow = 0;
        int currentCol = 0;
        int endRow = n - 1;
        int endCol = n - 1;
        int roundNum = 1;

        Console.WriteLine();

        while (roundNum <= n * n)
        {
            // current row till end cow - go right
            for (int i = currentRow; i <= endCol; i++)
            {
                matrix[i, currentRow] = roundNum;
                roundNum++;
            }
            currentRow++;

            // end row till end col - go down
            for (int i = currentRow; i <= endCol; i++)
            {
                matrix[endCol, i] = roundNum;
                roundNum++;
            }
            endCol--;

            // end col till current col - go left
            for (int i = endCol; i >= currentCol; i--)
            {
                matrix[i, endRow] = roundNum;
                roundNum++;
            }
            endRow--;

            // end col till current row - go up
            for (int i = endCol; i >= currentRow; i--)
            {
                matrix[currentCol, i] = roundNum;
                roundNum++;
            }
            currentCol++;
        }
        PrintMarix(n, matrix);
        Console.WriteLine();
 
    }

    private static void EnterCol(int n, int[,] matrix, ref int k, int i)
    {
        for (int j = 0; j < n; j++)
        {
            matrix[j, i] = k;
            k++;
        }
    }

    private static void EnterColReverse(int n, int[,] matrix, ref int k, int i)
    {
        for (int j = n-1; j >= 0; j--)
        {
            matrix[j, i] = k;
            k++;
        }
    }

    private static void PrintMarix(int n, int[,] matrix)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                Console.Write("{0,-3} ", matrix[i, j]);
            }
            Console.WriteLine();
        }
    }


}

